﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace market
{
    public partial class Admin : System.Web.UI.Page
    {
        CommonClass ccObj = new CommonClass();
        DBClass dbObj = new DBClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                check_Login();
                gvAdminBind();
            }
        }
        public void check_Login()
        {
            if ((Session["AName"] == null))
            {
                Response.Write("<script>alert('对不起！您不是管理员，无权限浏览此页！');location='Login.aspx'</script>");
                Response.End();
            }
        }
        public void gvAdminBind()
        {
            string sqlStr = "select * from Admin";
            DataTable dsTable = dbObj.GetDataSetStr(sqlStr, "tbAdmin");
            this.gvAdminList.DataSource = dsTable.DefaultView;
            this.gvAdminList.DataKeyNames = new string[] { "AdminID" };
            this.gvAdminList.DataBind();
        }
        protected void gvAdminList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAdminList.PageIndex = e.NewPageIndex;
            gvAdminBind();
        }
        protected void gvAdminList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int IntID = Convert.ToInt32(gvAdminList.DataKeys[e.RowIndex].Value.ToString());
            string strSql = "select count(*) from OrderInfo where AdminID=" + IntID;
            SqlCommand myCmd = dbObj.GetCommandStr(strSql);
            if (Convert.ToInt32(dbObj.ExecScalar(myCmd)) != 0)
            {
                Response.Write(ccObj.MessageBox("该用户名正被使用，无法删除！"));
                return;
            }
            else
            {
                string sqlDelStr = "delete from Admin where AdminID=" + IntID;
                SqlCommand myDelCmd = dbObj.GetCommandStr(sqlDelStr);
                dbObj.ExecNonQuery(myDelCmd);
                gvAdminBind();

            }

        }
    }
}